<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">
        <i class="el-icon-s-promotion"/> 首页
      </el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>新增SPU</el-breadcrumb-item>
    </el-breadcrumb>
    <el-divider/>
    <el-steps :active="2" align-center finish-status="success">
      <el-step title="步骤1" description="选择商品类别"></el-step>
      <el-step title="步骤2" description="填写商品的基本信息"></el-step>
      <el-step title="步骤3" description="选择商品相册"></el-step>
    </el-steps>
    <el-divider/>
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="商品相册">
        <el-select v-model="ruleForm.albumId" placeholder="请选择">
          <el-option
              v-for="item in albumListOptions"
              :key="item.id"
              :label="item.name"
              :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="warning" @click="backToPreStep()">上一步</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
        <el-button type="primary" @click="submitForm('ruleForm')">下一步</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      albumListOptions: [
        {id: 1, name: '手机'},
        {id: 2, name: '笔记本'},
        {id: 3, name: '数码'},
      ],
      ruleForm: {
        albumId: '',
        albumName: ''
      },
      // 验证规则
      rules: {
        albumId: [
          {required: true, message: '请输入商品类别的ID', trigger: 'blur'},
        ]
      }
    };
  },
  methods: {
    backToPreStep() {
      // 返回上一步
      this.$router.push('/sys-admin/product/spu/add-new-2');
    },
    loadLocalRuleForm() {
      let localRuleFormString = localStorage.getItem('localRuleFrom');
      if (!localRuleFormString) {
        this.$router.push('/sys-admin/product/spu/add-new-1');
      }
      let localRuleForm = JSON.parse(localRuleFormString);
      console.log(localRuleForm);
      console.log(localRuleForm.brandId)
      if (!localRuleForm.categoryId) {
        this.$router.push('/sys-admin/product/spu/add-new-1');
      } else if (!localRuleForm.brandId) {
        this.$router.push('/sys-admin/product/spu/add-new-2');
      }
      this.ruleForm = localRuleForm;
    },
    loadAlbumList() {
      // 定义后端接口
      let url = 'http://localhost:9080/albums';
      // 在控制台打印url的数值
      console.log("url=" + url);
      // 使用axios发送GET请求到url，并在获取响应后执行then方法
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .get(url).then((response) => {
        // 从响应中获取数据赋值给responseData变量
        let responseData = response.data;
        // 如果responseData中的state属性值等于2000
        if (responseData.state === 2000) {
          // 将responseData中的data属性值赋给this.albumListOptions
          this.albumListOptions = responseData.data;
        }
      })
    },
    submitForm(formName) {
      // 执行全部的检查规则
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = 'http://localhost:9080/albums/' + this.ruleForm.albumId;
          console.log('url = ' + url)
          this.axios
              .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
              .get(url).then((response) => {
            let responseBody = response.data;
            if (responseBody.state === 2000) {
              let album = responseBody.data;
              this.ruleForm.albumId = album.id;
              this.ruleForm.albumName = album.name;
              console.log(album);
              console.log('即将传输的数据 = '+ this.ruleForm)
              localStorage.setItem('localRuleForm', JSON.stringify(this.ruleForm));
              this.$router.push('/sys-admin/product/spu/add-new-4');
              // }
            } else {
              this.$message.error(responseBody.message);
            }
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  },
  mounted() {
    this.loadLocalRuleForm();
    this.loadAlbumList();
  }
}
</script>
<style>

</style>